import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import os
import warnings
root_path = os.path.abspath('../../')

font1 = {
    'family' : 'Times New Roman',
'weight' : 'normal',
'size'   : 16,
}

fontlegend = {
    'family' : 'Times New Roman',
'weight' : 'normal',
'size'   : 14,
}

data_path = root_path + "/asset/mergingData100m.csv"
ori_data = pd.read_csv(data_path)

mergingData = ori_data[
    (ori_data['MergingState'] == True)
    & (ori_data['RouteClass'] == 'entry')
    & (ori_data['BreakRuleState'] == 'None')
    & (ori_data['MergingDistance'] != 'None')
    & (ori_data['MaxLateralSpeed'] != 'None')
    & (ori_data['MergingType'] != 'None')
    & (ori_data['MergingType'] != 'G')
    & (ori_data['MergingType'] != 'H')
    & (ori_data['MergingType'] != 'A')
    & (ori_data['vehicleClass'] == 'car')
    ]

employedColumns = ['MergingDistanceRatio', 'totalvelocity', 'MergingDuration',
                   'trafficFlowArea4','trafficFlowArea5',"MergingType" ]

mergingData['MergingDistance'] = mergingData['MergingDistance'].astype('float')
mergingData['MergingDuration'] = mergingData['MergingDuration'].astype('float')
mergingData['MaxLateralSpeed'] = mergingData['MaxLateralSpeed'].astype('float')
mergingData['MaxiLateralAcc'] = mergingData['MaxiLateralAcc'].astype('float')
mergingData['MergingDistanceRatio'] = mergingData['MergingDistanceRatio'].astype('float')
mergingData['totalvelocity'] = np.sqrt(np.square(mergingData['xVelocity'])+np.square(mergingData['yVelocity']))
mergingData.sort_values(by="MergingType", inplace=True)

plt.figure(figsize=(14, 8))
plt.style.use('seaborn-colorblind')

ax1 = plt.subplot(221)
g1 = sns.scatterplot(x="MergingDistanceRatio", y="trafficFlowArea4", data=mergingData,hue = "MergingType",palette="Paired_r",style="MergingType")
plt.yticks([0,200,400,600,800,1000,1200,1400,1600,1800,2000])
plt.ylim([0,2000])
plt.xticks([0,0.2,0.4,0.6,0.8,1])
plt.ylabel("flow(veh/h)", font1)
plt.xlabel("distance ratio", font1)
plt.grid(ls='-', axis="both")
plt.title("location2"+"-Upstream", font1)
plt.legend(prop=fontlegend,ncol=2)

ax2 = plt.subplot(222)
g2 = sns.scatterplot(x="MergingDuration", y="trafficFlowArea4", data=mergingData,hue = "MergingType",palette="Paired_r",style="MergingType")
plt.yticks([0,200,400,600,800,1000,1200,1400,1600,1800,2000])
plt.ylim([0,2000])
plt.xticks([0,2,4,6,8,10])
plt.xlim([0,10])
plt.ylabel("flow(veh/h)", font1)
plt.xlabel("duration(s)", font1)
plt.grid(ls='-', axis="both")
plt.title("location2"+"-Upstream", font1)
plt.legend(prop=fontlegend,ncol=2)

ax3 = plt.subplot(223)
g3 = sns.scatterplot(x="MergingDistanceRatio", y="trafficFlowArea5", data=mergingData,hue = "MergingType",palette="Paired_r",style="MergingType")
plt.yticks([0,200,400,600,800,1000,1200,1400,1600,1800,2000])
plt.ylim([0,2000])
plt.xticks([0,0.2,0.4,0.6,0.8,1])
plt.ylabel("flow(veh/h)", font1)
plt.xlabel("distance ratio", font1)
plt.grid(ls='-', axis="both")
plt.title("location2"+"-Downstream", font1)
plt.legend(prop=fontlegend,ncol=2)

ax4 = plt.subplot(224)
g4 = sns.scatterplot(x="MergingDuration", y="trafficFlowArea5", data=mergingData, hue = "MergingType",palette="Paired_r",style="MergingType")
plt.yticks([0,200,400,600,800,1000,1200,1400,1600,1800,2000])
plt.ylim([0,2000])
plt.xticks([0,2,4,6,8,10])
plt.xlim([0,10])
plt.ylabel("flow(veh/h)", font1)
plt.xlabel("duration(s)", font1)
plt.grid(ls='-', axis="both")
plt.title("location2"+"-Downstream", font1)
plt.legend(prop=fontlegend,ncol=2)

ax2.get_legend().remove()
ax3.get_legend().remove()
ax4.get_legend().remove()

plt.tight_layout()
plt.show()

